<?php

namespace app\libs;

use function GuzzleHttp\Psr7\str;
use think\facade\Config;
use think\facade\Log;

/**
 * 腾讯移动分析
 * Class Qqmta
 * @package app\libs
 */
class Qqmta
{
    protected $appid;

    protected $eventid;

    protected $secret_key;

    protected $apiurl;

    public function __construct()
    {
        $config = Config::get('system.qq_mta');
        $this->appid = $config['appid'];
        $this->eventid = $config['eventid'];
        $this->secret_key = $config['secret_key'];
        $this->apiurl = $config['apiurl'];
    }

    /**
     * 生成签名
     * @param array $params
     * @return array
     */
    public function makeSign($params = [])
    {
        $secret_key = $this->secret_key;
        $params['app_id'] = $this->appid;
        $params['timestamp'] = time();
        ksort($params);
        foreach ($params as $key => $value) {
            $secret_key .= '&' . $key . '=' . $value;
        }
        $params['sign'] = md5($secret_key);
        return $params;
    }

    /**
     * 检测结果
     * @param $res
     * @return array
     */
    public function checkResult($res)
    {
        $resObj = json_decode($res, true);
        if (isset($resObj['data'])) {
            return ['code' => 1, 'data' => $resObj['data']];
        } else {
            return ['code' => 0, 'msg' => $resObj['message']];
        }
    }

    /**
     * 实时数据
     * @return mixed
     */
    public function getRealTime()
    {
        $params = $this->makeSign();
        $url = $this->apiurl . '/analytics/real_time' . '?' . http_build_query($params);
//        Log::info($url);
        $res = Util::httpGet($url);
//        Log::info($res);
        return $this->checkResult($res);
    }

    /**
     * 历史趋势
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function getHistory($start_time, $end_time)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/history' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }

    /**
     * 新老用户
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function getUserCompare($start_time, $end_time)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/user_compare' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }

    /**
     * 分享分析
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function getShare($start_time, $end_time)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/share' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }

    /**
     * 地域分析
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function getArea($start_time, $end_time)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/area' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }

    /**
     * 终端分析
     * @param $start_time
     * @param $end_time
     * @param $type
     * @return array
     */
    public function getTerminal($start_time, $end_time, $type)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'type' => $type
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/terminal' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }

    /**
     * 网络分析
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function getNetwork($start_time, $end_time)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/network' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }

    /**
     * 使用时段分析
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function getPeriod($start_time, $end_time)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/period' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }

    /**
     * 机型分析
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function getMachine($start_time, $end_time)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/machine' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }

    # 销售分析
    public function getSale($start_time, $end_time){
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        $params = $this->makeSign($param);
        dump($params);die;
    }

    /**
     * 自定义事件详情
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function getEventDetail($start_time, $end_time)
    {
        $param = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'event_id' => $this->eventid
        ];
        $params = $this->makeSign($param);
        $url = $this->apiurl . '/analytics/event_detail' . '?' . http_build_query($params);
        $res = Util::httpGet($url);
        return $this->checkResult($res);
    }
}